package com.baselibrary.test.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.baselibrary.libs.Utils.log.LogUtils;
import com.baselibrary.libs.View.bar.initAction_Bar;
import com.baselibrary.libs.View.defined.defined_tab_layou.Defined_tab_layou;
import com.baselibrary.libs.View.defined.defined_viewpager.Defined_ViewPager;
import com.baselibrary.libs.View.defined.defined_viewpager.fragment.Fragment_item_01;
import com.baselibrary.libs.View.defined.defined_viewpager.fragment.Fragment_item_02;
import com.baselibrary.libs.View.defined.defined_viewpager.fragment.Fragment_item_03;
import com.baselibrary.libs.View.defined.defined_viewpager.fragment.Fragment_item_04;
import com.baselibrary.libs.View.kalaokText.TextView_kalaok_Plus;
import com.baselibrary.libs.base.baseActivity.BaseFragmentActivity;
import com.baselibrary.libs.example.example_viewpager_fragment.view.MyViewPagerAdapter;
import com.baselibrary.test.R;
import com.baselibrary.test.app.AppContext;

import java.util.ArrayList;


public class viewpager_fragment_pull_02 extends BaseFragmentActivity {

    private Defined_ViewPager mViewPager;
    private initAction_Bar action_bar;
    private MyViewPagerAdapter mAdapter;
    private TextView_kalaok_Plus mTv_01;
    private TextView_kalaok_Plus mTv_02;
    private TextView_kalaok_Plus mTv_03;
    private TextView_kalaok_Plus mTv_04;
    private ArrayList<TextView_kalaok_Plus> mPluses;
    private Defined_tab_layou mTabLayout;
    private ArrayList<TextView_kalaok_Plus> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getlayouXML() {
        return R.layout.activity_viewpager_fragment_02;
    }

    @Override
    public void initView() {
        action_bar = (initAction_Bar)findViewById(R.id.action_bar);

        mViewPager = (Defined_ViewPager)findViewById(R.id.defined_ViewPager);

        mTabLayout = (Defined_tab_layou)findViewById(R.id.tabLayout);

        mTabLayout.addTab(mTabLayout.newTab().setText("MyTab 1"));
        mTabLayout.addTab(mTabLayout.newTab().setText("MyTab 2"));
        mTabLayout.addTab(mTabLayout.newTab().setText("MyTab 9999"));
        mTabLayout.addTab(mTabLayout.newTab().setText("MyTab 4"));
        mTabLayout.addTab(mTabLayout.newTab().setText("MyTab 1"));
        mTabLayout.addTab(mTabLayout.newTab().setText("MyTab 2"));
        mTabLayout.addTab(mTabLayout.newTab().setText("MyTab 9999"));
        mTabLayout.addTab(mTabLayout.newTab().setText("MyTab 4"));
//        mTabLayout.addTab(mTabLayout.newTab().setText("MyTab 1"));
//        mTabLayout.addTab(mTabLayout.newTab().setText("MyTab 2"));
//        mTabLayout.addTab(mTabLayout.newTab().setText("MyTab 9999"));
//        mTabLayout.addTab(mTabLayout.newTab().setText("MyTab 4"));
//        mTabLayout.addTab(mTabLayout.newTab().setText("MyTab 1"));
//        mTabLayout.addTab(mTabLayout.newTab().setText("MyTab 2"));
//        mTabLayout.addTab(mTabLayout.newTab().setText("MyTab 9999"));
//        mTabLayout.addTab(mTabLayout.newTab().setText("MyTab 4"));
//        mTabLayout.addTab(mTabLayout.newTab().setTextView_kalaok_Plus(new TextView_kalaok_Plus(AppContext.getApplication())));
//        mTabLayout.addTab(mTabLayout.newTab().setTextView_kalaok_Plus(new TextView_kalaok_Plus(AppContext.getApplication())));
//        mTabLayout.addTab(mTabLayout.newTab().setTextView_kalaok_Plus(new TextView_kalaok_Plus(AppContext.getApplication())));
//        mTabLayout.addTab(mTabLayout.newTab().setTextView_kalaok_Plus(new TextView_kalaok_Plus(AppContext.getApplication())));
//        mTabLayout.getTabAt(0).setCustomView(getTabView(0));
//        mTabLayout.getTabAt(1).setCustomView(getTabView(1));
//        mTabLayout.getTabAt(2).setCustomView(getTabView(2));
//        mTabLayout.getTabAt(3).setCustomView(getTabView(0));

        action_bar.set_Title("viewPage2");

//        mTv_01 = (TextView_kalaok_Plus)findViewById(R.id.tv_01);
//        mTv_02 = (TextView_kalaok_Plus)findViewById(R.id.tv_02);
//        mTv_03 = (TextView_kalaok_Plus)findViewById(R.id.tv_03);
//        mTv_04 = (TextView_kalaok_Plus)findViewById(R.id.tv_04);
//
//        mTv_01.setOnClickListener(this);
//        mTv_02.setOnClickListener(this);
//        mTv_03.setOnClickListener(this);
//        mTv_04.setOnClickListener(this);
//        mPluses = new ArrayList<>();
//
//        mPluses.add(mTv_01);
//        mPluses.add(mTv_02);
//        mPluses.add(mTv_03);
//        mPluses.add(mTv_04);
        initData();
    }

    private View getTabView(int i) {
        TextView_kalaok_Plus plus = new TextView_kalaok_Plus(AppContext.getApplication());
        return plus;
    }

    @Override
    public void initP() {

    }

    @Override
    public void initData() {
        Fragment_item_01 item_01 = new Fragment_item_01();
        Fragment_item_02 item_02 = new Fragment_item_02();
        Fragment_item_03 item_03 = new Fragment_item_03();
        Fragment_item_04 item_04 = new Fragment_item_04();
        Fragment_item_01 item_05 = new Fragment_item_01();
        Fragment_item_02 item_06 = new Fragment_item_02();
        Fragment_item_03 item_07 = new Fragment_item_03();
        Fragment_item_04 item_08 = new Fragment_item_04();
        Fragment_item_01 item_09 = new Fragment_item_01();
        Fragment_item_02 item_10 = new Fragment_item_02();
        Fragment_item_03 item_11 = new Fragment_item_03();
        Fragment_item_04 item_12 = new Fragment_item_04();
        final ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(item_01);
        fragments.add(item_02);
        fragments.add(item_03);
        fragments.add(item_04);
        fragments.add(item_05);
        fragments.add(item_06);
        fragments.add(item_07);
        fragments.add(item_08);
//        fragments.add(item_09);
//        fragments.add(item_10);
//        fragments.add(item_11);
//        fragments.add(item_12);
        mViewPager.setData(getSupportFragmentManager(),fragments);

        if (fragments.size()<4){
            mTabLayout.setTabMode(Defined_tab_layou.MODE_FIXED);

        }else{
            mTabLayout.setTabMode(Defined_tab_layou.MODE_SCROLLABLE);

        }
        //mTv_01.setWith(0,true);
        mList = mTabLayout.getList();
        mList.get(0).setWith(0,true);
        // mTabLayout.setTabsFromPagerAdapter(mViewPager.getAdapter());


        mTabLayout.setOnTabSelectedListener(new Defined_tab_layou.OnTabSelectedListener() {
            @Override
            public void onTabSelected(Defined_tab_layou.Tab var1) {
                int position = var1.getPosition();

                LogUtils.i("被选中的数字是"+position);
                for (int i = 0; i < mList.size(); i++) {
                    if (position==i){
                        mList.get(position).setWith(0,true);
                        mViewPager.getMyViewPger().setCurrentItem(position,false);
                    }else{
                        mList.get(i).setWith(0,false);
                    }
                }
            }

            @Override
            public void onTabUnselected(Defined_tab_layou.Tab var1) {

            }

            @Override
            public void onTabReselected(Defined_tab_layou.Tab var1) {

            }
        });

        mViewPager.getMyViewPger().addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                LogUtils.i("滑动_onPageScrolled\t"+"滑动开始的时候的页\t"+position+"\tpositionOffset\t"+positionOffset+"\tpositionOffset\t"+positionOffset);
                boolean b1 = true;
                boolean b2 = true;
                mTabLayout.setScrollPosition(position,positionOffset,true);
                if (0.0!=positionOffset){
                    int i = (int) (positionOffset*100);

                    if (i <= 100) {
                        mList.get(position).setWith(i,b1);
                        mList.get(position+1).setWith(i-100,b2);

                    }


                }
            }

            @Override
            public void onPageSelected(int position) {
                LogUtils.i("滑动_的页\t"+position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                LogUtils.i("滑动_的状态\t"+state);

            }
        });

    }

    @Override
    public void initListener() {

    }

//    @Override
//    public void onClick(View v) {
//        switch (v.getId()){
//            case R.id.tv_01:
//                mViewPager.getMyViewPger().setCurrentItem(0,false);
//                mTv_01.setWith(0,true);
//                mTv_02.setWith(0,false);
//                mTv_03.setWith(0,false);
//                mTv_04.setWith(0,false);
//            break;
//            case R.id.tv_02:
//                mViewPager.getMyViewPger().setCurrentItem(1,false);
//                mTv_02.setWith(0,true);
//                mTv_01.setWith(0,false);
//                mTv_03.setWith(0,false);
//                mTv_04.setWith(0,false);
//            break;
//            case R.id.tv_03:
//                mViewPager.getMyViewPger().setCurrentItem(2,false);
//                mTv_03.setWith(0,true);
//                mTv_02.setWith(0,false);
//                mTv_01.setWith(0,false);
//                mTv_04.setWith(0,false);
//            break;
//            case R.id.tv_04:
//                mViewPager.getMyViewPger().setCurrentItem(3,false);
//                mTv_04.setWith(0,true);
//                mTv_02.setWith(0,false);
//                mTv_03.setWith(0,false);
//                mTv_01.setWith(0,false);
//            break;
//        }
//    }
}
